package com.ai.wxy.spring.user.exception;

import com.ai.spring.common.enums.ExceptionEnum;
import com.ai.spring.common.util.ResponseResult;
import com.ai.spring.common.util.ResponseResultUtil;
import com.ai.spring.common.util.ThrowableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 控制器错误处理
 *
 * @author 石头
 * @Date 2019/10/28
 * @Version 1.0
 **/
@Slf4j
@RestControllerAdvice
public class RestControllerExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult<String> validException(MethodArgumentNotValidException exception){
        // 打印堆栈信息
        log.error("validException:{}",ThrowableUtil.getStackTrace(exception));

        return ResponseResultUtil.ofError(ExceptionEnum.PARAM_BIND_ERROR);
    }
    @ExceptionHandler(BindException.class)
    public ResponseResult<String> bindException(BindException exception){
        // 打印堆栈信息
        log.error("BindException:{}",ThrowableUtil.getStackTrace(exception));

        return ResponseResultUtil.ofError(ExceptionEnum.PARAM_BIND_ERROR);
    }
}
